
import trash from '@/apis/trash'
import {Message} from 'element-ui'
const state = {
    trashNotes: null,
    curTrashNoteId:null,
}
const getters = {
    trashNotes: state => state.trashNotes || [],
    curTrashNote: (state,getters) => {
        if (!state.curTrashNoteId) { return getters.trashNotes[0] || {} }
        return state.trashNotes.find((trashNote=>trashNote.id==state.curTrashNoteId))||{}
    },
    belongTo: (state, getters, rootState, rootGetters) => {
        let notebook = rootGetters.notebooksList.find(notebook => notebook.id == getters.curTrashNote.notebookId) || {}
        return notebook.title || ''
      }
}
const mutations = {
    setCurTrashNote(state, payload) {
        state.curTrashNoteId=payload.curTrashNoteId
    },
    addTrashNote(state, payload) {
        state.trashNotes.unshift(payload.trashNote)
    },
    deleteTrashNote(state, payload) {
        state.trashNotes=state.trashNotes.filter(trashNote=>trashNote.id!=payload.trashNoteId)
    },
    setTrashNotes(state, payload) {
        state.trashNotes = payload.trashNotes;
    }

}
const actions = {
    getAll({commit}) {
        return trash.getAll().then((res) => {
            commit('setTrashNotes',{trashNotes:res.data})
        })
    },
    revertTrashNote({commit},{trashNoteId}) {
       return trash.revertNote({ noteId: trashNoteId }).then((res) => {
            commit('deleteTrashNote', { trashNoteId: trashNoteId })
            Message.success(res.msg)
        })
    }
    ,
    deleteTrashNote({commit},{trashNoteId}) {
      return  trash.deleteNote({noteId:trashNoteId}).then((res) => {
            commit('deleteTrashNote', { trashNoteId: trashNoteId })
            Message.success(res.msg)
        })
    },
}
export default {
    state,
    getters,
    mutations,
    actions
}